#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash lumpy-0.3.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget

set -e
yum install numpy zlib-devel autoconf automake openssl-devel -y

cd $buildpath
if `wget -t 40 -c -P $buildpath  https://ghproxy.com/https://github.com/arq5x/lumpy-sv/archive/refs/tags/v0.3.1.tar.gz`
then 
   echo "v0.3.1.tar.gz下载成功!(国内github加速下载)"
elif `wget -t 40 -c -P $buildpath  https://github.com/arq5x/lumpy-sv/archive/refs/tags/v0.3.1.tar.gz`
then 
   echo "v0.3.1.tar.gz下载成功!"
else
   echo "v0.3.1.tar.gz下载失败!"
fi
if `wget -t 40 -c -P $buildpath  https://ghproxy.com/https://github.com/samtools/htslib/archive/d2d9c76ade2df2b63b9cf79ae8decda1dfadc042.zip`
then 
   echo "d2d9c76ade2df2b63b9cf79ae8decda1dfadc042.zip下载成功!(国内github加速下载)"
elif `wget -t 40 -c -P $buildpath  https://github.com/samtools/htslib/archive/d2d9c76ade2df2b63b9cf79ae8decda1dfadc042.zip`
then 
   echo "d2d9c76ade2df2b63b9cf79ae8decda1dfadc042.zip下载成功!"
else
   echo "d2d9c76ade2df2b63b9cf79ae8decda1dfadc042.zip下载失败!"
fi
if `wget -t 40 -c -P $buildpath  https://ghproxy.com/https://github.com/hall-lab/bamkit/archive/148ef1d3ac05ef7a033ed5ef42749c21956718d6.zip`
then 
   echo "148ef1d3ac05ef7a033ed5ef42749c21956718d6.zip下载成功!(国内github加速下载)"
elif `wget -t 40 -c -P $buildpath  https://github.com/hall-lab/bamkit/archive/148ef1d3ac05ef7a033ed5ef42749c21956718d6.zip`
then 
   echo "148ef1d3ac05ef7a033ed5ef42749c21956718d6.zip下载成功!"
else
   echo "148ef1d3ac05ef7a033ed5ef42749c21956718d6.zip下载失败!"
fi
tar xf v0.3.1.tar.gz && rm -rf v0.3.1.tar.gz
unzip d2d9c76ade2df2b63b9cf79ae8decda1dfadc042.zip && rm -rf d2d9c76ade2df2b63b9cf79ae8decda1dfadc042.zip
unzip 148ef1d3ac05ef7a033ed5ef42749c21956718d6.zip && rm -rf 148ef1d3ac05ef7a033ed5ef42749c21956718d6.zip

cp -rf htslib-d2d9c76ade2df2b63b9cf79ae8decda1dfadc042/* lumpy-sv-0.3.1/lib/htslib/
cp -rf bamkit-148ef1d3ac05ef7a033ed5ef42749c21956718d6/* lumpy-sv-0.3.1/scripts/bamkit/

cd lumpy-sv-0.3.1
sed -i 's/ g++/ clang++/g' Makefile
sed -i "25s/$/&-march=armv8.2-a -fsigned-char -ffp-contract=off/" Makefile
make -j$(nproc)
mkdir -p $installpath/lumpy-0.3.1 && cp -r $buildpath/lumpy-sv-0.3.1/* $installpath/lumpy-0.3.1

cat>"$installpath/lumpy-0.3.1/lumpy_modulefiles"<<EOF
#%Module1.0
conflict lumpy
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set LUMPY \$pwd
setenv LUMPY \$LUMPY
prepend-path PATH \$LUMPY/bin
EOF

rm -rf $buildpath/lumpy-sv-0.3.1

